<template>
  <view>
    <view @click="downloadFileOpen">excel下载</view>
  </view>
</template>

<script setup>
function downloadFileOpen() {
  //   let link = "https://psy-public.obs.myhuaweicloud.com/bpai/static/bpai-wxapp-test-file/测试xls.xlsx";
  //   let link = "https://psy-public.obs.myhuaweicloud.com/bpai/static/bpai-wxapp-test-file/测试word.docx";
  let link =
    "https://psy-public.obs.myhuaweicloud.com/bpai/static/bpai-wxapp-test-file/测试.pdf";
  let filename = link.substring(link.lastIndexOf("/") + 1);
  console.log("wx.env.USER_DATA_PATH---", wx.env.USER_DATA_PATH);
  console.log("filename---", filename);
  //下载文件
  wx.downloadFile({
    url: link,
    filePath: wx.env.USER_DATA_PATH + "/" + filename,
    success(res) {
      if (res.statusCode === 200) {
        const filePath = res.filePath;
        console.log("res---", res);
        console.log("filePath---", filePath);
        wx.openDocument({
          filePath: filePath,
          showMenu: true, //关键点
          success: function (res) {
            console.log("打开文档成功");
          },
          fail(){
             console.log('文件打开失败');
          }
        });
      }
    },
    fail(){
        console.log('文件下载失败');
    },
    complete(){
        console.log('文件下载操作完成');
    },
  });
}
</script>
<style lang="scss" scoped></style>
